package com.jie.template.impl;

import com.jie.constants.RabbitMQConstant;
import com.jie.constants.RedisConstants;
import com.jie.po.EmailCodePO;
import com.jie.utils.RandomUtils;
import com.jie.utils.UserUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

import static com.jie.constants.CommonConst.EMAIL_TEMPLATE;

/**
 * @Author: 杰
 * @Project :net-disk
 * @DateTime: 2024/5/6 11:28
 **/
@Service("loginCaptchaEmailImpl")
public class LoginCaptchaEmailImpl extends AbstractEmailManage{
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    public void generateEmailContent(EmailCodePO emailCodePO) {
        //发送登录验证码
        // 1.生成验证码
        String captcha = RandomUtils.generateCaptcha();
        emailCodePO.setText(EMAIL_TEMPLATE+captcha);
        // 发送HTML邮件
        rabbitTemplate.convertAndSend(RabbitMQConstant.EMAIL_CODE_EXCHANGE, RabbitMQConstant.EMAIL_CODE_KEY, emailCodePO);
        // 3. 并且redis存储一份验证码来进行验证 300S有效
        redisTemplate.opsForValue().set(RedisConstants.EMAIL_LOGIN_CODE+ emailCodePO.getTo(),captcha,RedisConstants.EMAIL_LOGIN_CODE_EXPIRE, TimeUnit.SECONDS);
    }
}
